package com.example.testdemo.liveing;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;


/**
 * Created by 颜 on 04.13.013.
 * 直播中动画
 */

public class LiveIngView extends View {
    private Paint mPaint;
    private RectF mRectF;
    private RectF mRectF2;
    private float strokeWidth;
    private Rect bounds;
    private String string;
    private Handler mHandler;
    private Runnable mRunnable;
    private int alpha;
    private boolean isAddAlpha;

    public LiveIngView(Context context) {
        super(context);
    }

    public LiveIngView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        strokeWidth = 6;
        string = "直播中";
        initHandler();
    }

    public LiveIngView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mRectF = new RectF(0, 0, getWidth(), getHeight());
        strokeWidth = getHeight() / 20;
        mRectF2 = new RectF(strokeWidth, strokeWidth, getWidth() - strokeWidth, getHeight() - strokeWidth);
        bounds = new Rect();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //画背景
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.BLACK);
        mPaint.setAlpha(100);
        canvas.drawRoundRect(mRectF, 5000, 5000, mPaint);
        //画边框
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(strokeWidth);
        mPaint.setAlpha(alpha);
        canvas.drawRoundRect(mRectF2, 5000, 5000, mPaint);
        //画字
        float textSize = getHeight() - getHeight() / 2.5f;
        float baseline = getHeight() - textSize / 2;
        mPaint.getTextBounds(string, 0, string.length(), bounds);
        mPaint.setTextSize(textSize);
        mPaint.setStrokeWidth(2);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAlpha(255);
        mPaint.setTextAlign(Paint.Align.LEFT);
        canvas.drawText(string, getWidth() / 2 + getWidth() / 10, baseline, mPaint);
        //画圆点
        mPaint.setColor(Color.RED);
        mPaint.setAlpha(355 - alpha);
        canvas.drawCircle(getHeight() / 2, getHeight() / 2, getHeight() / 5, mPaint);

        startGradientAnimation();
    }


    private void initHandler() {
        isAddAlpha = true;
        alpha = 0;
        mHandler = new Handler();
        mRunnable = new Runnable() {
            @Override
            public void run() {
                if (isAddAlpha) {
                    alpha = alpha + 10;
                    if (alpha >= 250) {
                        isAddAlpha = false;
                    }
                } else {
                    alpha = alpha - 10;
                    if (alpha <= 100) {
                        isAddAlpha = true;
                    }
                }
                postInvalidate();
            }
        };
    }

    private void startGradientAnimation() {
        mHandler.postDelayed(mRunnable, 50);
    }
}
